package com.huike.web.controller.report;

import com.huike.business.domain.TbBusiness;
import com.huike.business.mapper.TbBusinessMapper;
import com.huike.business.service.ITbBusinessService;
import com.huike.clues.domain.TbClue;
import com.huike.clues.mapper.SysDeptMapper;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.clues.service.ITbClueService;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.contract.domain.TbContract;
import com.huike.contract.mapper.TbContractMapper;
import com.huike.contract.service.ITbContractService;
import com.huike.web.controller.clues.TbClueController;
import org.hibernate.validator.internal.util.Contracts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.huike.common.core.controller.BaseController;

import java.util.*;


@RestController
@RequestMapping("/report")
public class ReportController extends BaseController {

    @Autowired
    TbClueMapper clueMapper;
    @Autowired
    TbBusinessMapper businessMapper;
    @Autowired
    TbContractMapper contractMapper;
    @Autowired
    SysDeptMapper sysDeptMapper;

    @GetMapping("/getVulnerabilityMap/{beginCreateTime}/{endCreateTime}")
    public AjaxResult clues(@PathVariable Date beginCreateTime ,@PathVariable Date endCreateTime){

        int cluesNums = clueMapper.selectAllClues(beginCreateTime, endCreateTime);
        int effectiveCluesNums = clueMapper.selectAllUseClues(beginCreateTime, endCreateTime);
        int businessNums = businessMapper.selectAllTbBusiness(beginCreateTime, endCreateTime);
        int contractNums = contractMapper.selectAllTbContract(beginCreateTime, endCreateTime);

        Map<String, Object> map = new HashMap<>();
        map.put("cluesNums",cluesNums);
        map.put("effectiveCluesNums",effectiveCluesNums);
        map.put("businessNums",businessNums);
        map.put("contractNums",contractNums);

       return AjaxResult.success(map);
    }



    @GetMapping("/deptStatisticsList/{beginCreateTime}/{endCreateTime}")
    public TableDataInfo list(@PathVariable String beginCreateTime , @PathVariable String endCreateTime){
        List<TbContract> tbContracts = contractMapper.AllTbContract(beginCreateTime, endCreateTime);
        int num=0;
        int amount = 0;
        Map<String, Object> map = new HashMap<>();
        List<Map<String, Object>> list = new ArrayList<>();
        for (TbContract tbContract : tbContracts) {
            String deptId = tbContract.getDeptId().toString();
            int i = Integer.parseInt(deptId);
            if (!map.containsKey(deptId)) {
                map.put("dept_id",deptId);
                num += 1;
                String deptName = sysDeptMapper.getDeptName(i);
                map.put("num",num);
                map.put("deptName",deptName);
               amount += tbContract.getOrder();
                map.put("total_amount",amount);
            }else {
                num ++;
                amount += tbContract.getOrder();
                map.put("num",num);
                map.put("total_amount",amount);
            }
        }
        Map<String, Object> map2=new HashMap<>();
        map2.put("rows",map);
        list.add(map2);
        return getDataTablePage(list);
    }

}